package com.tianji.learning.controller;

import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.pojo.LearningPlanDTO;
import com.tianji.learning.pojo.LearningPlanPageVO;
import com.tianji.learning.service.ILearningLessonService;
import com.tianji.learning.vo.LearningLessonVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学生课程表 控制器
 * </p>
 *
 * @author xjs
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/lessons")
public class LearningLessonController {

    private final ILearningLessonService learningLessonService;

    @GetMapping("/page")
    @ResponseBody
    public PageDTO<LearningLessonVO> page(PageQuery query){
        return learningLessonService.pageQuery(query);
    }

    @GetMapping("now")
    @ResponseBody
    public LearningLessonVO now(){
        return learningLessonService.now();
    }

    @ApiOperation("分页查询学习计划")
    @GetMapping("/plans")
    public LearningPlanPageVO getPlans(PageQuery pageQuery){
        return learningLessonService.getPlans(pageQuery);
    }

    @ApiOperation("设置学习计划")
    @PostMapping("/plans")
    public void setPlans(@Validated @RequestBody LearningPlanDTO dto){
        learningLessonService.setPlans(dto);
    }

    @GetMapping("/{courseId}")
    @ApiOperation("根据课程id获取课表信息")
    public LearningLessonVO getLesson(@PathVariable("courseId") Long courseId){
        Long userId = UserContext.getUser();
        return learningLessonService.getLessonByUserIdCourseId(userId, courseId);
    }

    @DeleteMapping("/{courseId}")
    @ApiOperation("删除指定课程信息")
    public void deleteCourseFromLesson(
            @ApiParam(value = "课程id" ,example = "1") @PathVariable("courseId") Long courseId) {
        learningLessonService.deleteCourseFromLesson(null, courseId);
    }

    @ApiOperation("统计课程学习人数")
    @GetMapping("/{courseId}/count")
    public Integer countLearningLessonByCourse(
            @ApiParam(value = "课程id" ,example = "1") @PathVariable("courseId") Long courseId){
        return learningLessonService.countLearningLessonByCourse(courseId);
    }

    @ApiOperation("校验当前课程是否已经报名")
    @GetMapping("/{courseId}/valid")
    public Long isLessonValid(
            @ApiParam(value = "课程id" ,example = "1") @PathVariable("courseId") Long courseId){
        return learningLessonService.isLessonValid(courseId);
    }




}
